package work;
import java.net.*;
import java.io.*;
import java.util.*;

public class URLConnectionExample {

    public static void main(String args[]) throws Exception {
        // 创建一个URL对象，获取命令行参数中的URL
        URL url = new URL(args[0]);

        // 打开与URL相关的连接
        URLConnection con = url.openConnection();

        // 打印一些关于URL连接的信息
        System.out.println("URL used is: " + con.getURL().toExternalForm());
        System.out.println("Content Type: " + con.getContentType());
        System.out.println("Content Length: " + con.getContentLength());
        System.out.println("Last Modified: " + new Date(con.getLastModified()));

        // 打印网页的前10行内容
        System.out.println("First Ten Lines:");

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

        // 读取并打印前10行
        for (int i = 0; i < 10; i++) {
            String line = in.readLine();
            if (line == null) {
                break; // 如果没有更多的行了，退出循环
            }
            System.out.println(" " + line);
        }

        // 关闭BufferedReader
        in.close();
    }
}
